package cn.gson.springboot.model.service.cusservice;

import cn.gson.springboot.model.dao.cusdao.CuclueDao;
import cn.gson.springboot.model.dao.cusdao.CulinkmanDao;
import cn.gson.springboot.model.dao.cusdao.CustomerDao;
import cn.gson.springboot.model.mapper.cusmapper.CuclueMapper;
import cn.gson.springboot.model.pjos.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.List;

@Service
@Transactional
public class CuclueService {
    @Autowired
    CuclueMapper cuclueMapper;//线索mapper
    @Autowired
    CuclueDao cuclueDao;//线索dao
    @Autowired
    CustomerDao customerDao;//客户dao
    @Autowired
    CulinkmanDao culinkmanDao;//联系人dao

    //    分页查询线索
    public List<Cuclue> pageSelectCuclue(Integer empId,Integer deptId,String cucStatus,String serch){
        if(cucStatus != null && cucStatus != "") {
            if (cucStatus.equalsIgnoreCase("全部线索")) {
                cucStatus = "";
                System.out.println("cucStatus2:" + cucStatus);
            }
        }
        return cuclueMapper.pageSelectCuclue(empId,deptId,1,cucStatus,serch);
    }

//    新增、修改线索
    public void saveCuclue(Cuclue cuclues,Integer empId){
        System.out.println("线索负责人："+empId);
        cuclues.setCucStatus("2");
        Emp emp=new Emp();
        emp.setEmployeesId(empId);
        cuclues.setCulueEmpid(emp);
        cuclueDao.save(cuclues);
    }

//    设为已失效线索
    public void updateCulue(Integer cucId){
        Cuclue cuclue=new Cuclue();
        cuclue.setCucStatus("0");
        cuclueMapper.updateCulue(cuclue.getCucStatus(),cucId);
    }

//    将线索新增为客户
    public void addCustomer(Cuclue cuclue,String cusAddressProvince,String cusAddressCity,String cusAddressArea){
        System.err.println("cusAddressProvince:"+cusAddressProvince);
        CustomerType customerType=new CustomerType();//客户类型
        customerType.setTypeId(1);//默认为潜在客户
        Customer customer = new Customer();
        customer.setCusState(1);
        customer.setCusSource("其他途径");
        customer.setAttention(0);
        customer.setTypeId(customerType);//默认为潜在客户
        customer.setCusAddressProvince(cusAddressProvince);//客户地址
        customer.setCusAddressCity(cusAddressCity);
        customer.setCusAddressArea(cusAddressArea);
        customer.setCusName(cuclue.getCucGather());//客户名
        customer.setCusRemark(cuclue.getCucRemark());//备注
//        设置客户的负责人
        customer.setCustomEmpid(cuclue.getCulueEmpid());
        customerDao.save(customer);//添加客户操作
        System.err.println("customer:"+customer);
        //        设置客户的联系人
        Culinkman culinkman=new Culinkman();
        culinkman.setCustomerByCustomId(customer);//联系人的对应客户
        culinkman.setCupState(1);//有效
        culinkman.setCupMaster(1);//设置为首要联系人
        culinkman.setCupName(cuclue.getCucLinkName());//联系人姓名
        culinkman.setCupPhone(cuclue.getCucLinkPhone());//联系人电话
        culinkman.setCupAddressProvince(cuclue.getCuclinkProvince());//地址
        culinkman.setCupAddressCity(cuclue.getCuclinkCity());
        culinkman.setCupAddressArea(cuclue.getCuclinkArea());
        culinkman.setCupXxaddress(cuclue.getCuclinkXxaddress());
        //            添加联系人操作
        culinkmanDao.save(culinkman);
        System.err.println("culinkman:"+culinkman);

//        修改该线索为已处理
        cuclue.setCucStatus("1");
//        将该线索的对应客户修改为刚新增的客户
        cuclue.setCustomerByCustomId(customer);
        cuclueDao.save(cuclue);
    }

    //    查询线索联系电话是否重复
    public Cuclue selectCucCupPhone(String cucLinkPhone){
        return cuclueMapper.selectCucCupPhone(cucLinkPhone);
    }
}
